const {
  handleCancelRequestApproved,
} = require('./handlers/cancel');

const {
  handleOrderCreated,
  handleOrderPaid,
  handleOrderCanceled,
} = require('./handlers/order');

const {
  handleRoomStatusUpdated,
  handleRoomDeleted,
} = require('./handlers/room');

const {
  handleUserRegistered,
  handleUserLoginSuccess,
  handleUserVerified,
} = require('./handlers/user');

const registerEventHandlers = (io) => {
  io.on('connection', (socket) => {
    console.log(`[WebSocket] 用户 ${socket.user.username} 已连接`);

    // 取消请求
    handleCancelRequestApproved(io, socket);

    // 订单相关
    handleOrderCreated(io, socket);
    handleOrderPaid(io, socket);
    handleOrderCanceled(io, socket);

    // 会议室相关
    handleRoomStatusUpdated(io, socket);
    handleRoomDeleted(io, socket);

    // 用户相关
    handleUserRegistered(io, socket);
    handleUserLoginSuccess(io, socket);
    handleUserVerified(io, socket);

    // 断开连接
    socket.on('disconnect', () => {
      console.log(`[WebSocket] 用户 ${socket.user.username} 已断开`);
    });

    // 所有事件监听（调试用）
    socket.onAny((event, ...args) => {
      console.log(`[WebSocket] 收到事件: ${event}`, args);
    });
  });
};

module.exports = { registerEventHandlers };